{extend name="layout" /}
{block name="body"}
<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-body">
                <div id="toolbar" class="toolbar-btn-action">
                    <button id="btn_delete" type="button" class="btn btn-label btn-danger">
                        <label><i class="mdi mdi-window-close" aria-hidden="true"></i></label>删除
                    </button>
                    <button type="button" class="btn btn-label btn-danger" onclick="del_order_all()">
                        <label><i class="mdi mdi-window-close" aria-hidden="true"></i></label>删除所有待付款订单
                    </button>
                </div>

                <table id="tb_departments"></table>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="js"}
<script>
    $('#tb_departments').bootstrapTable({
        classes: 'table table-bordered table-hover table-striped',
        url: "{:url('shop/getshoporderlist')}",
        method: 'get',
        dataType: 'json',
        uniqueId: 'id',
        idField: 'id',             // 每行的唯一标识字段
        toolbar: '#toolbar',       // 工具按钮容器
        clickToSelect: true,     // 是否启用点击选中行
        showColumns: true,         // 是否显示所有的列
        showRefresh: true,         // 是否显示刷新按钮
        showToggle: true,        // 是否显示详细视图和列表视图的切换按钮(clickToSelect同时设置为true时点击会报错)
        pagination: true,                    // 是否显示分页
        sortOrder: "desc",                    // 排序方式
        queryParams: function (params) {
            var temp = {
                limit: params.limit,         // 每页数据量
                offset: params.offset,       // sql语句起始索引
                page: (params.offset / params.limit) + 1,
                sort: params.sort,           // 排序的列名
                sortOrder: params.order,      // 排序方式'asc' 'desc'
            };
            return temp;
        },                                   // 传递参数
        sidePagination: "server",            // 分页方式：client客户端分页，server服务端分页
        pageNumber: 1,                       // 初始化加载第一页，默认第一页
        pageSize: 10,                        // 每页的记录行数
        pageList: [10, 25, 50, 100],         // 可供选择的每页的行数
        //search: true,                      // 是否显示表格搜索，此搜索是客户端搜索

        showExport: true,        // 是否显示导出按钮, 导出功能需要导出插件支持(tableexport.min.js)
        exportDataType: "basic", // 导出数据类型, 'basic':当前页, 'all':所有数据, 'selected':选中的数据

        columns: [{
            field: 'example',
            checkbox: true    // 是否显示复选框
        }, {
            field: 'id',
            title: 'id',
            sortable: true    // 是否排序
        }, {
            field: 'order_no',
            title: '订单号',
        }, {
            field: 'transaction_no',
            title: '交易号',
        }, {
            field: 'name',
            title: '商品名称',
        }, {
            field: 'type',
            title: '商品类型',
            formatter: function (value, row, index) {
                var value = "";
                if (row.type == '1') {
                    value = '<span>兑换会员</span>';
                } else {
                    value = '<span>其他类型</span>';
                }
                return value;
            }
        }, {
            field: 'price',
            title: '价格',
            sortable: true
        }, {
            field: 'appname',
            title: 'app名称',
        }, {
            field: 'username',
            title: '用户名',
        }, {
            field: 'paymenttime',
            title: '支付时间',
        }, {
            field: 'createtime',
            title: '创建时间',
        }, {
            field: 'status',
            title: '状态',
            formatter: function (value, row, index) {
                var value = "";
                if (row.status == 0) {
                    value = '<span class="badge badge-danger">待付款</span>';
                } else {
                    value = '<span class="badge badge-success">已付款</span>';
                }
                return value;
            }
        }, {
            field: 'payment',
            title: '支付方式',
            formatter: function (value, row, index) {
                var value = "";
                if (row.payment == 0) {
                    value = '<span class="badge badge-success">支付宝支付</span>';
                } else if (row.payment == 1) {
                    value = '<span class="badge badge-success">金币支付</span>';
                } else if (row.payment == 2) {
                    value = '<span class="badge badge-success">积分支付</span>';
                } else if (row.payment == 3) {
                    value = '<span class="badge badge-success">源支付</span>';
                } else {
                    value = '<span class="badge badge-success">易支付</span>';
                }
                return value;
            }
        }, {
            field: 'operate',
            title: '操作',
            formatter: btnGroup,  // 自定义方法
            events: {
                'click .del-btn': function (event, value, row, index) {
                    deleteorder(row.id, row.order_no);
                }
            }
        }],
        onLoadSuccess: function (data) {
            $("[data-toggle='tooltip']").tooltip();
        }
    });

    // 操作按钮
    function btnGroup() {
        let html =
            '<a href="#!" class="btn btn-xs btn-default del-btn" title="删除" data-toggle="tooltip"><i class="mdi mdi-window-close"></i></a>';
        return html;
    }

    //获取选中的id
    function getnumber() {
        var selRows = $('#tb_departments').bootstrapTable("getSelections");
        if (selRows.length == 0) {
            notify.success("请至少选择一行");
            return false;
        }
        var postData = "";
        $.each(selRows, function (i) {
            postData += this.id;
            if (i < selRows.length - 1) {
                postData += ",";
            }
        });
        return postData;
    }

    //删除
    function deleteorder(id, order_no) {
        $.confirm({
            title: '确认删除',
            content: "确定删除" + order_no + "此订单?",
            autoClose: 'cancelAction|10000',
            escapeKey: 'cancelAction',
            buttons: {
                confirm: {
                    btnClass: 'btn-red',
                    text: '删除' + order_no + '此订单',
                    action: function () {
                        var l = $('body').lyearloading({
                            opacity: 0.2,
                            spinnerSize: 'lg',
                            spinnerText: '后台处理中，请稍后...',
                            textColorClass: 'text-info',
                            spinnerColorClass: 'text-info'
                        });
                        $.ajax({
                            type: "POST",
                            url: "{:url('shop/deleteorder')}",
                            data: {
                                id: id,
                            },
                            dataType: "json",
                            success: function (data) {
                                l.destroy();
                                notify.success(data.msg);
                                $('#tb_departments').bootstrapTable('refresh');
                            },
                            error: function () {
                                l.destroy();
                                notify.error("系统错误");
                            }
                        })
                    }
                },
                cancelAction: {
                    text: '取消',
                    action: function () {
                    }
                }
            }
        });
    }


    $("#btn_delete").click(function () {
        var getnum = getnumber();
        //console.log(getnum);
        if (getnum == false) {
            return false;
        }
        var l = $('body').lyearloading({
            opacity: 0.2,
            spinnerSize: 'lg',
            spinnerText: '后台处理中，请稍后...',
            textColorClass: 'text-info',
            spinnerColorClass: 'text-info'
        });
        $.ajax({
            type: "POST",
            url: "{:url('shop/deleteorder')}",
            data: {
                id: getnum
            },
            dataType: "json",
            success: function (data) {
                l.destroy();
                notify.success(data.msg);
                $('#tb_departments').bootstrapTable('refresh');
            },
            error: function () {
                l.destroy();
                notify.error("系统错误");
            }
        });
    });

    function del_order_all() {
        $.confirm({
            title: '确认删除',
            content: "确定删除所有待付款订单?",
            autoClose: 'cancelAction|10000',
            escapeKey: 'cancelAction',
            buttons: {
                confirm: {
                    btnClass: 'btn-red',
                    text: '确定删除所有待付款订单',
                    action: function () {
                        var l = $('body').lyearloading({
                            opacity: 0.2,
                            spinnerSize: 'lg',
                            spinnerText: '后台处理中，请稍后...',
                            textColorClass: 'text-info',
                            spinnerColorClass: 'text-info'
                        });
                        $.ajax({
                            type: "POST",
                            url: "{:url('shop/deleteorderall')}",
                            data: {},
                            dataType: "json",
                            success: function (data) {
                                l.destroy();
                                notify.success(data.msg);
                                $('#tb_departments').bootstrapTable('refresh');
                            },
                            error: function () {
                                l.destroy();
                                notify.error("系统错误");
                            }
                        })
                    }
                },
                cancelAction: {
                    text: '取消',
                    action: function () {
                    }
                }
            }
        });
    }
</script>
{/block}